package com.ltmonitor.service.impl;

import com.ltmonitor.util.DateUtil;
import com.ltmonitor.vo.AGpsResult;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

/**
 * 调用百度API和高德地图实现地理位置的逆向编码查询 需要申请百度地图和高德地图开发key
 * 
 * @author Administrator
 *         参见百度API说明:http://developer.baidu.com/map/webservice-geocoding.htm
 *         返回码定义 0 正常 1 服务器内部错误 2 请求参数非法 3 权限校验失败 4 配额校验失败 5 ak不存在或者非法 101 服务禁用
 *         102 不通过白名单或者安全码不对 2xx 无权限 3xx 配额错误
 */
@Service("aGpsService")
public class AGpsService implements com.ltmonitor.service.IAGpsService {

	protected Logger log = Logger.getLogger(getClass());


	/**
	 * 调用次数计数器
	 */
	private long times = 0;

	public AGpsService() {

	}


	/**
	 * 调用基站查询接口
	 */
	public AGpsResult getLocationByAGPS(int MCC, int MNC, long LAC, long CID) {
		Date start = new Date();
		URLConnection connection = null;
		OutputStreamWriter out = null;
		BufferedReader in = null;
		try {
			URL url = new URL(" http://api.cellocation.com:81/cell/?mcc="+ MCC + "&mnc="+ MNC + "&lac="+ LAC+"&ci="+CID+ "&output=json");

			// System.out.println(url.toString());
			connection = url.openConnection();
			connection.setConnectTimeout(3000);
			connection.setReadTimeout(3000);
			HttpURLConnection httpURLConnection = (HttpURLConnection) connection;

			int code = httpURLConnection.getResponseCode();
			if (code == 450) {
				log.error("API返回450错误");
				return null;
			}
			String res;
			InputStream l_urlStream;
			l_urlStream = connection.getInputStream();
			in = new BufferedReader(new InputStreamReader(l_urlStream, "UTF-8"));
			StringBuilder sb = new StringBuilder("");
			while ((res = in.readLine()) != null) {
				sb.append(res.trim());
			}
			String responseString = sb.toString();
			// System.out.println(responseString);

			Date end = new Date();

			// log.error("定位信息耗时:" + DateUtil.getSeconds(start, end));
			JSONObject jsonArr = JSONObject.fromObject(responseString);
			int status = jsonArr.getInt("errcode");
			if (status == 0) {
				double lat = jsonArr.getDouble("lat");
				double lng = jsonArr.getDouble("lon");
				int radius = jsonArr.getInt("radius");
				String address = jsonArr.getString("address");
				AGpsResult ap = new AGpsResult();
				ap.setLatitude(lat);
				ap.setLongitude(lng);
				ap.setRadius(radius);
				ap.setAddress(address);
				return ap;
			} else {
				//10000: 参数错误
				//10001: 无查询结果
				// String info = jsonArr.getString("info");
				log.error(url);
				log.error("解析错误,错误码:" + status + ",文本:" + responseString);
			}

		} catch (Exception ex) {
			log.info("解析错误,发生异常:" + ex.getMessage());
		} finally {
			try {
				if (out != null)
					out.close();
			} catch (Exception ex) {

			}

			try {
				if (in != null)
					in.close();
			} catch (Exception ex) {

			}

		}
		return null;

	}

	public static void main(String[] args) throws IOException {

		AGpsService ls = new AGpsService();
		Date start = new Date();
		for (int m = 0; m < 1; m++) {
			AGpsResult ap = ls.getLocationByAGPS(460, 1,4301,20986);
			System.out.println("location:" + ap.toString());
		}
		Date end = new Date();
		double seconds = DateUtil.getSeconds(start, end);
		System.out.println("耗时:" + seconds);

		// System.out.println(location);
	}

}
